package com.skt.common_hr.teamworktime_sac.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.teamworktime_sac.TeamWorkTimeFacade;
	import com.skt.common_hr.teamworktime_sac.model.vo.MemberVO;
	import com.wcl.utils.StringUtil;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	

	public class MemberSearchProxy extends CommonProxy
	{
		public static const NAME:String = "MemberSearchProxy";
		
		public var memberList:ArrayCollection;
		
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function MemberSearchProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function load(orgeh:String, keyword:String):void
		{
			var param:Object = new Object();
			param["I_ORGEH"] = orgeh;
			param["I_KEYWORD"] = keyword;
			
			loadData("Z_HR_0016", param, result, fault); 
		}

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function result(event:ResultEvent):void
		{
			if(retCode==0) {  
				memberList = parseMemberList(event.result.RTAB);
				facade.sendNotification(TeamWorkTimeFacade.LOAD_NAMESEARCH_COMPLETE, this);
			}
			else {
				trace( "failed to get org list: " + event.result["RETCODE"] + ", " + event.result["RETTEXT"] );
				facade.sendNotification(TeamWorkTimeFacade.LOAD_NAMESEARCH_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function fault(event:FaultEvent):void
		{
			trace(event.fault.faultString);
			facade.sendNotification(TeamWorkTimeFacade.LOAD_NAMESEARCH_FAILED, this);
		}
		
		
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 인사기록 Value Object
		 */
		private function parseMemberList(retObj:ArrayCollection):ArrayCollection
		{
			var list:ArrayCollection = new ArrayCollection();
			for each(var rec:Object in retObj)
			{
				var vo:MemberVO = new MemberVO();
				vo.ORGEH 		= rec.ORGEH;
				vo.STEXT 		= rec.STEXT;
				vo.PERNR 		= StringUtil.trimFront(rec.PERNR, "0");
				vo.ENAME 		= rec.ENAME;
				vo.PLANST 		= rec.PLANST;
				vo.PTEXT 		= rec.PTEXT;
				vo.STELLT 		= rec.STELLT;
				vo.JWTXT 		= rec.JWTXT;
				vo.TEL_OFFICE 	= rec.TEL_OFFICE;
				vo.TEL_MOBILE1	= rec.TEL_MOBILE1;
				vo.TEL_MOBILE2 	= rec.TEL_MOBILE2;
				vo.WKADDR		= rec.WKADDR;
				vo.PHOTO_URL 	= rec.PHOTO_URL;
				if(vo.PHOTO_URL.length<1) {
					vo.PHOTO_URL = "assets/images/noimage.swf";
				}
				vo.BAND 		= rec.BAND;
				vo.PERSK 		= rec.PERSK;
				list.addItem(vo);
			}
			return list;
		}
		
	}
}